package com.haixiaoke.flat.lock.controller;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.lock.domain.LockPwd;
import com.haixiaoke.saas.lock.service.ILockPwdService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 锁密码授权Controller
 * 
 * @author panda
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/front/flat/lock/pwd")
public class FlatLockPwdController extends BaseController {
    @Autowired
    private ILockPwdService lockPwdService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询锁密码授权列表
     */
    @GetMapping("/page")
    public TableDataInfo list(LockPwd lockPwd) {
        if (lockPwd.getLockId() == null) {
            return getDataTable("请选择锁");
        }
        startPage();
        List<LockPwd> list = lockPwdService.selectLockPwdList(lockPwd);
        return getDataTable(list);
    }

    /**
     * 新增锁密码授权
     */
    @AuthCheck
    @PostMapping
    public AjaxResult add(@RequestBody LockPwd lockPwd) {
        lockPwd.setUserName(frontTokenComponent.getLoginUser().getUser().getUserName());
        return lockPwdService.insertLockPwd(lockPwd, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 密码授权禁用/启用
     */
    @AuthCheck
    @PutMapping("/editPwdState/{pwdId}")
    public AjaxResult editPwdState(@PathVariable("pwdId") Long pwdId) {
        return lockPwdService.editPwdState(pwdId);
    }

    /**
     * 删除锁密码授权
     */
    @AuthCheck
	@DeleteMapping("/{pwdId}")
    public AjaxResult remove(@PathVariable Long pwdId) {
        return lockPwdService.deleteLockPwdByPwdId(pwdId);
    }
}
